package com.petRescueCenter.demo.voluntary.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.petRescueCenter.demo.common.enumPackage.LockedEmun;
import com.petRescueCenter.demo.user.entity.User;
import com.petRescueCenter.demo.voluntary.dao.UserVoluntaryDao;
import com.petRescueCenter.demo.voluntary.dao.VoluntaryDao;
import com.petRescueCenter.demo.voluntary.dao.VolunteerDao;
import com.petRescueCenter.demo.voluntary.entity.UserVoluntary;
import com.petRescueCenter.demo.voluntary.entity.Voluntary;
import com.petRescueCenter.demo.voluntary.entity.Volunteer;
import com.petRescueCenter.demo.voluntary.entity.DTO.VoluntaryDTO;

@Service
@Transactional
public class VoluntaryService implements IVoluntaryService {
	@Autowired
	private VoluntaryDao voluntaryDao;
	@Autowired
	private VolunteerDao volunteerDao;
	@Autowired
	private UserVoluntaryDao userVoluntaryDao;

	@Override
	public Voluntary save(Voluntary entity) {
		// TODO Auto-generated method stub
		return voluntaryDao.save(entity);
	}

	@Override
	public Voluntary findById(Long id) {
		// TODO Auto-generated method stub
		return voluntaryDao.findById(id).get();
	}

	@Override
	public boolean existsById(Long id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void deleteById(Long id) {
		// TODO Auto-generated method stub
		voluntaryDao.deleteById(id);
	}

	@Override
	public void deleteAll(Long[] ids) {
		// TODO Auto-generated method stub

	}

	@Override
	public Page<VoluntaryDTO> findAll(Long userId, Specification<Voluntary> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		Page<Voluntary> pages = voluntaryDao.findAll(spec, pageable);
		System.err.println(spec.toString());
		List<VoluntaryDTO> Lists = null;
		if (pages != null) {
			Lists = new ArrayList<VoluntaryDTO>();

			for (Voluntary entity : pages) {
				int count = 0;
				VoluntaryDTO dto = new VoluntaryDTO();
				dto.entityToDto(entity, dto);
				count = userVoluntaryDao.getCountByVoluntaryId(dto.getId());
				dto.setCount(count);
				Volunteer ve = volunteerDao.findByUserId(userId);
				if (userVoluntaryDao.findByTwoId(dto.getId(), ve.getId()) != null) {
					dto.setIsjoin(LockedEmun.YES);
				} else {
					dto.setIsjoin(LockedEmun.NO);
				}
				Lists.add(dto);
			}
		}
		return new PageImpl<VoluntaryDTO>(Lists, pageable, pages.getTotalElements());
	}

	public boolean isEnd(Long voluntaryId) {
		Voluntary v = voluntaryDao.findById(voluntaryId).get();
		return v.getEndTime().after(new Date());
	}

	@Override
	public boolean existsByName(String name) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public User findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<VoluntaryDTO> getVoluntary(Specification<UserVoluntary> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		Page<UserVoluntary> pages = userVoluntaryDao.findAll(spec, pageable);
		List<VoluntaryDTO> Lists = null;
		if (pages != null) {
			Lists = new ArrayList<VoluntaryDTO>();
			for (UserVoluntary entity : pages) {
				VoluntaryDTO dto = new VoluntaryDTO();
				dto.UserVoluntarytoVoluntary(entity, dto);
				Lists.add(dto);
			}
		}
		return new PageImpl<VoluntaryDTO>(Lists, pageable, pages.getTotalElements());
	}

}
